/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_logs_keywords.h"
#include "rep_logs_pages.h"
#include "rep_general.h"
#include "GR_write_html.h"

void
write_body_topkey_html_file(GraphFrame *gf,FILE *fp,int nkey,WebStatPtr msn);

void
report_keywords_search_engine(FILE *fp,int nkey,WebStatPtr msn,
			      char *name_search_engine, int se,
			      LNode_type *lkeys,int (*isl)(),int (*isb)());

int 
report_top_keywords(GraphFrame *gf, char *name_file,int nkey,
		    WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Top Keywords");
  write_start_body_html_file(fp,"#ffffff");
  write_body_topkey_html_file(gf,fp,nkey,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_topkey_html_file(GraphFrame *gf,FILE *fp,int nkey,WebStatPtr msn)
{
  Hashtable_type *key_table = msn ? LOGR_get_table_keywords(msn) : NULL;
  LNode_type *lkeys = key_table ? get_list_elements(*key_table) : NULL;
  LNode_type *ptr = NULL;
  WebKeywordPtr *varray = NULL;
  int tnkeys = lkeys ? count_llist(lkeys) : 0;
  int i=0;
  int tn=0;
  char ntitle[100];
  time_t now;

  long thits = LOGR_get_total_site_naccesses(msn);
  long tsearch = LOGR_get_total_number_searches(msn);

  varray = tnkeys ? (WebKeywordPtr *)calloc(tnkeys,sizeof(WebKeywordPtr)) : NULL;

  if(!varray && tnkeys > 0) {
      fprintf(stderr,"Out of Memory write_body_pmopreq_html_file\n");
      abort();
  }

  for(ptr = lkeys->back; ptr != lkeys; ptr=ptr->back)
  {
    WebKeywordPtr mk = (WebKeywordPtr)ptr->info;
    varray[i++] = mk;
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Top Keywords");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Searches : ");
  fprintf(fp, " %ld \n",tsearch);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Keywords : ");
  fprintf(fp, " %d \n",tnkeys);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RTKString); 
  write_horizontal_rule_html_file(fp);

  if(tnkeys > 0) {
  if(nkey > tnkeys)
      tn = tnkeys;
  else
      tn = nkey;

  sprintf(ntitle,"%d Top Keywords", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Num", "Keyword","Count","Percentage");

  for(i = 1; i<=tn ; i++)
  {
    WebKeywordPtr mk = (WebKeywordPtr)
	randomized_select((GPTR *)varray,i-1,tnkeys-1,1,
			  LOGR_is_total_count_keyword_lower,
			  LOGR_is_total_count_keyword_bigger);
    if(mk)
    {
      const char *keyword = LOGR_get_keyword(mk);
      int tcount = LOGR_get_total_count(mk);
      float ave_nacc = ((float)tcount/(float)tsearch)*100.0;
      char snum[20],stcount[20],save[20];
      sprintf(snum,"%d",i);
      sprintf(stcount,"%d",tcount);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      if(keyword)
	  write_row_num_string3(fp,snum,keyword,stcount,save);
    }	
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;

  report_keywords_search_engine(fp,nkey,msn,SALTAVISTA, TALTAVISTA,
				lkeys,LOGR_is_se_count1_keyword_lower,
				LOGR_is_se_count1_keyword_bigger);

  report_keywords_search_engine(fp,nkey,msn,SYAHOO, TYAHOO,
				lkeys,LOGR_is_se_count2_keyword_lower,
				LOGR_is_se_count2_keyword_bigger);

  report_keywords_search_engine(fp,nkey,msn,SYAHOO2, TYAHOO2,
				lkeys,LOGR_is_se_count3_keyword_lower,
				LOGR_is_se_count3_keyword_bigger);

  report_keywords_search_engine(fp,nkey,msn,SGOOGLE, TGOOGLE,
				lkeys,LOGR_is_se_count4_keyword_lower,
				LOGR_is_se_count4_keyword_bigger);

  report_keywords_search_engine(fp,nkey,msn,SGOOGLE2, TGOOGLE2,
				lkeys,LOGR_is_se_count5_keyword_lower,
				LOGR_is_se_count5_keyword_bigger);

  report_keywords_search_engine(fp,nkey,msn,SINFOSEEK, TINFOSEEK,
				lkeys,LOGR_is_se_count6_keyword_lower,
				LOGR_is_se_count6_keyword_bigger);

  report_keywords_search_engine(fp,nkey,msn,SLYCOS, TLYCOS,
				lkeys,LOGR_is_se_count7_keyword_lower,
				LOGR_is_se_count7_keyword_bigger);

  Delete_all_list(lkeys); 

}

void
report_keywords_search_engine(FILE *fp,int nkey,WebStatPtr msn,
			      char *name_search_engine, int se,
			      LNode_type *lkeys,int (*isl)(),int (*isb)())
{
  LNode_type *ptr = NULL;
  int tn = 0;
  int tnkeys = 0;
  int i = 0;
  WebKeywordPtr *varray = NULL;
  char ntitle[100];
  long tsearch = LOGR_get_total_number_searches(msn);

  for(ptr = lkeys->back; ptr != lkeys; ptr=ptr->back)
  {
    WebKeywordPtr mk = (WebKeywordPtr)ptr->info;
    int count = LOGR_get_search_engine_count(mk,se);
    if(count > 0)
	tnkeys++;
  }

  varray = tnkeys ? (WebKeywordPtr *)calloc(tnkeys,sizeof(WebKeywordPtr)) : NULL;

  if(!varray && tnkeys > 0) {
      fprintf(stderr,"Out of Memory report_keywords_search_engine\n");
      abort();
  }

  for(ptr = lkeys->back; ptr != lkeys; ptr=ptr->back)
  {
    WebKeywordPtr mk = (WebKeywordPtr)ptr->info;
    int count = LOGR_get_search_engine_count(mk,se);
    if(count > 0)
	varray[i++] = mk;
  }
  
  if(tnkeys > 0) {
  if(nkey > tnkeys)
      tn = tnkeys;
  else
      tn = nkey;

  sprintf(ntitle,"%d Top Keywords from %s", tn,name_search_engine);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Num", "Keyword","Count","Percentage");

  for(i = 1; i<=tn ; i++)
  {
    WebKeywordPtr mk = (WebKeywordPtr)
	randomized_select((GPTR *)varray,i-1,tnkeys-1,1,isl,isb);
    if(mk)
    {
      const char *keyword = LOGR_get_keyword(mk);
      int tcount = LOGR_get_search_engine_count(mk,se);
      float ave_nacc = ((float)tcount/(float)tsearch)*100.0;
      char snum[20],stcount[20],save[20];
      sprintf(snum,"%d",i);
      sprintf(stcount,"%d",tcount);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      if(keyword)
	  write_row_num_string3(fp,snum,keyword,stcount,save);
    }	
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;

}

